package isd.com.smarthome;

import isd.com.smarthome.adapter.SwitchAdapter;
import isd.com.smarthome.model.Query;
import isd.com.smarthome.model.Room;
import isd.com.smarthome.model.Switches;
import isd.com.smarthome.provider.SmartHomeProvider;

import java.util.ArrayList;

import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class DetailsFragment extends BaseFragment{
//	private ExpandableListView exp_listRoom;
	private ListView lv_list;
	private TextView tv_room;
	private Room room;
	private Query stquery;
	private ArrayList<Switches> array;

	public Query getQuery() {
		return stquery;
	}

	public void setQuery(Query query) {
		stquery = query;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room iroom) {
		room = iroom;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootview = inflater.inflate(R.layout.detailfragment, container,false);
//		exp_listRoom = (ExpandableListView) rootview.findViewById(R.id.exp_listroom);
		lv_list =(ListView) rootview.findViewById(R.id.exp_listroom);
		tv_room = (TextView) rootview.findViewById(R.id.tv_room);
		if (container != null) {			
			setView(rootview);
			tv_room.setVisibility(View.VISIBLE);
		}
		
		return rootview;
	}
	
	
	public void setView(View view){		
		Cursor data = getActivity().getContentResolver().query(SmartHomeProvider.SWITCHES_CONTENT_URI, null,SmartHomeProvider.SwitchTable.ID_ROOM+"="+room.getIdRoom(), null, null);
		 data.moveToFirst();
		 array = new ArrayList<Switches>();
		// lay data tu database
		try {
			
			while (!data.isAfterLast()) {
		            Switches switches = new Switches();
		            SmartHomeProvider.getSwitches(data, switches);
		            array.add(switches);
		            data.moveToNext();
		        } 
		    
		   
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(data!=null){
			 data.close();
			}
		}
		//
		SwitchAdapter adapter = new SwitchAdapter(getActivity(),R.layout.switchitem,array);
		lv_list.setAdapter(adapter);


	}
	
}
